package com.tutor.lzm.function.teacher.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListView;

import com.tutor.lzm.AppSession;
import com.tutor.lzm.R;
import com.tutor.lzm.activity.BaseActivity;
import com.tutor.lzm.bean.TeacherExperienceBean;
import com.tutor.lzm.function.teacher.adapter.TeacherExperienceAdapter;
import com.tutor.lzm.http.BaseResponse;
import com.tutor.lzm.http.HttpClient;
import com.tutor.lzm.http.request.TeacherExperienceRequest;
import com.tutor.lzm.http.response.TeacherExperienceList;
import com.tutor.syslib.Logs;
import com.tutor.syslib.util.MessageTools;
import com.tutor.syslib.util.TextUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zzgpc on 2017-9-23.
 * 教育经历list
 */
public class TeacherExperienceListAct extends BaseActivity {

    private String teacherId;
    private TeacherExperienceAdapter adapter;
    private List<TeacherExperienceBean> beanList = new ArrayList<TeacherExperienceBean>();
    private ListView listView;
    private boolean isEdit = false;

    public static void start(Context context, String teacherId) {
        Intent intent = new Intent(context, TeacherExperienceListAct.class);
        if (!TextUtil.isNull(teacherId)) {
            intent.putExtra("teacherId", teacherId);
        }
        context.startActivity(intent);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.hasExtra("refresh")) {
            getTeacherExperienceList();
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_list_view);
        listView = (ListView) findViewById(R.id.listView);
        if (getIntent().hasExtra("teacherId")) {
            teacherId = getIntent().getStringExtra("teacherId");
            initTitle("教育经历");
            isEdit = false;
        } else {
            initTitle("教育经历", "新增", rightListener);
            isEdit = true;
            teacherId = AppSession.getInstance().getUserId();
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    AddTeacherExperienceAct.start(oThis, beanList.get(position));
                }
            });
        }


        adapter = new TeacherExperienceAdapter(this, beanList, isEdit);
        listView.setAdapter(adapter);
        getTeacherExperienceList();
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventMessage(TeacherExperienceBean bean) {
        Logs.v(tag, "eventMessage   times");

        if (TextUtil.isNull(bean.teacherSeniorityId)) {
            beanList.add(bean);
            adapter.notifyDataSetChanged();
        } else {
            int size = beanList.size();
            for (int i = 0; i < size; i++) {
                if (beanList.get(i).teacherSeniorityId.equals(bean.teacherSeniorityId)) {
                    beanList.add(i, bean);
//                    adapter = new TeacherExperienceAdapter(this, beanList);
//                    listView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    return;
                }
            }
        }
    }


    @Override
    public void finish() {
        EventBus.getDefault().post(beanList);
        super.finish();

    }

    /**
     * 获取老师教育经历列表
     */
    private void getTeacherExperienceList() {
        final TeacherExperienceRequest request = new TeacherExperienceRequest();
        request.setTeacherId(teacherId);
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                try {
//                    TeacherExperienceList list = request;
                    beanList = (List<TeacherExperienceBean>) response;
                    adapter = new TeacherExperienceAdapter(TeacherExperienceListAct.this, beanList, isEdit);
                    listView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                } catch (Exception e) {
                    Logs.d(tag, e.getMessage());
                }


            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                Logs.d(tag, msg);
            }
        });
    }

    private View.OnClickListener rightListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddTeacherExperienceAct.start(TeacherExperienceListAct.this, null);
        }
    };
}
